package boot3.controller;
import boot3.pojo.*;
import boot3.service.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



@RestController
public class SystemController {
    //解决跨域问题
    @Configuration
    public class GlobalCorsConfig implements WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry corsRegistry) {
            // 允许跨域访问资源定义： / 所有资源
            corsRegistry.addMapping("/**")
                    // 将允许跨域访问的域名传给函数 allowedOrigins，支持配置多个域名
                    .allowedOrigins("http://localhost:5173","\n" +
                            "ws://localhost:5173")
                    // 允许发送Cookie
                    .allowCredentials(true)
                    // 允许所有方法
                    .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD");
        }
    }

    //创建服务对象
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ChatroomService chatroomService;
    @Autowired
    private LivechatService livechatService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private NoticeUserService noticeUserService;


    //通过ID获取指定的用户的信息
    @GetMapping("/findByID")
    public String userfindByID(User user,HttpSession session) throws JsonProcessingException {
        session.setAttribute("user",user.getUserID());
        //将用户的账号信息存储在session
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(userService.findByID(user.getUserID()));
    }

    //获取指定的学生的信息
    @GetMapping("/getStuById")
    public String getStuById(String student_id, HttpSession session) throws JsonProcessingException {
        session.setAttribute("user",student_id);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(studentService.getStudentById(student_id));
    }

    //获取所有的聊天室信息
    @GetMapping("/getAllChatrooms")
    public String getAllChatrooms(HttpSession session) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(chatroomService.findAll());
    }

    //创建新的聊天室信息
    @PostMapping("/createChatroom")
    public String createChatroom(@RequestBody Chatroom chatroom){
    return chatroomService.createChatroom(chatroom.getProject_name(),chatroom.getStart_time(),
            chatroom.getAdmin_id(),chatroom.isIs_private());
    }

    //根据聊天室的ID删除指定的聊天室
    @GetMapping("/deleteChatroom")
    public Boolean deleteChatroom(String chatroom_id){
        return chatroomService.deleteChatroom(chatroom_id) && livechatService.deleteAllUsers(chatroom_id);
    }

    //根据聊天室的ID获取聊天室的管理ID
    @GetMapping("/getLiveroomAdminID")
    public String getLiveroomAdminID(String chatroom_id) throws JsonProcessingException {
        return chatroomService.getLiveroomAdminID(chatroom_id);
    }

    //根据聊天室的ID获取聊天室的所有公告
    @GetMapping("/getChatroomNotice")
    public String getChatroomNotice(String chatroom_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(noticeService.findAllNotice(chatroom_id));
    }

    //在指定的聊天室中添加公告
    @PostMapping("/addNoticeInChatroom")
    public Boolean addNoticeInChatroom(@RequestBody Notice notice){
        return noticeService.addNotice(notice);
    }

    //根据指定的ID删除公告
    @GetMapping("/deleteNoticeInChatroom")
    public Boolean deleteNoticeInChatroom(String notice_id){
        return noticeService.deleteNotice(notice_id);
    }

    //获取指定聊天室中所有用户的姓名
    @GetMapping("/getAllUserName")
    public String getAllUserName(String chatroom_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(livechatService.getUsersName(chatroom_id));
    }

    //获取指定的聊天室
    @GetMapping("/getChatroomByID")
    public String getChatroomByID(String chatroom_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(chatroomService.findChatroom(chatroom_id));
    }

    //根据学生ID获取该学生的所有预约
    @GetMapping("/getAllReservationByStudentID")
    public String getAllReservationByStudentID(String student_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(reservationService.selectAllReservationByStudentID(student_id));
    }

    //根据老师的ID获取该老师的所有预约
    @GetMapping("/getAllReservationByTeacherID")
    public String getAllReservationByTeacherID(String teacher_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(reservationService.selectAllReservationByTeacherID(teacher_id));
    }

    //新建一条预约
    @PostMapping("/createReservation")
    public Boolean createReservation(@RequestBody Reservation reservation){
        return reservationService.createReservation(reservation);
    }

    //根据预约编号删除一条预约
    @GetMapping("/deleteReservation")
    public Boolean deleteReservation(String reserve_number){
        return reservationService.deleteReservation(reserve_number);
    }

    //根据预约编号修改一条预约
    @GetMapping("/updateReservation")
    public Boolean updateReservation(String reserve_number){
        return reservationService.updateState(reserve_number);
    }

    //根据预约编号驳回一条预约
    @GetMapping("/updateReservationStatePurpose")
    public Boolean updateReservationStatePurpose(String reserve_number,String purpose){
        return reservationService.updateStatePurpose(reserve_number,purpose);
    }

    //根据老师的姓名获取老师的ID
    @GetMapping("/getTeacherByName")
    public String getTeacherByName(String teacher_name) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(teacherService.getTeacher(teacher_name));
    }

    //修改学生的个人信息
    @GetMapping("/updateStuInfo")
    public Boolean updateStuInfo(String student_id,String item,String value){
        System.out.println(student_id);
        System.out.println(item);
        System.out.println(value);
        return studentService.updateStudent(student_id,item,value);
    }

    //修改老师的个人信息
    @GetMapping("/updateTeaInfo")
    public Boolean updateTeaInfo(String teacher_id,String item,String value){
        return teacherService.updateTeacher(teacher_id,item,value);
    }

    //获取学院的所有老师
    @GetMapping("/getTeachersByCollege")
    public String getTeachersByCollege(String college) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(teacherService.getTeachersByCollege(college));
    }

    //获取所有学院的所有学生
    @GetMapping("/getStudentsByCollege")
    public String getStudentsByCollege(String college) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(studentService.getStudentByCollege(college));
    }

    //根据message_id将次消息标注为已读状态
    @GetMapping("/updateMessageWatch")
    public Boolean updateMessageWatch(String message_id){
        return messageService.updateMessageWatch(message_id);
    }

    //根据接受者和发送者的姓名修改数据库中的所有的消息阅读情况
    @GetMapping("/updateMessageWatchByRS")
    public Boolean updateMessageWatchByRS(String receiver_name,String sender_name){
        return messageService.updateMessageWatchByRS(receiver_name,sender_name);
    }

    //根据学院获取该学院的所有大创信息
    @GetMapping("/getProjectByCollege")
    public String getProjectByCollege(String college) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(projectService.findProjectByCollege(college));
    }

    //插入一条申请信息
    @PostMapping("/addApplication")
    public Boolean addApplication(@RequestBody Application application){
        return applicationService.addApplication(application);
    }

    //根据学生ID获取这个学生所有的申请
    @GetMapping("/getApplicationByStudentID")
    public String getApplicationByStudentID(String student_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(applicationService.selectApplicationByStudentId(student_id));
    }

    //获取所有的科创项目
    @GetMapping("/getAllProjects")
    public String getAllProjects() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(projectService.findAllProject());
    }

    //获取所有的学院
    @GetMapping("/getAllCollege")
    public String getAllCollege() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(studentService.findCollege());
    }

    //根据老师的ID获取指定的老师
    @GetMapping("/getTeacherByID")
    public String getTeacherByID(String teacher_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(teacherService.getTeacherById(teacher_id));
    }
    //根据老师的ID获取这个老师管理的所有科创项目
    @GetMapping("/getProjectsByAdminID")
    public String getProjectsByAdminID(String admin_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(projectService.findProjectByTeacher(admin_id));
    }

    //根据项目的ID获取申请表
    @GetMapping("/getApplicationByProjectID")
    public String getApplicationByProjectID(String project_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(applicationService.selectApplicationByProjectId(project_id));
    }

    //根据项目的ID修改申请状态
    @GetMapping("/updateApplicationStateByApplicationID")
    public Boolean updateApplicationStateByApplicationID(String application_id){
        return applicationService.updateApplicationState(application_id);
    }

    //新增一个科创项目
    @PostMapping("/addProject")
    public Boolean addProject(@RequestBody Project project) {
        return projectService.addProject(project);
    }

    //删除一个大创
    @GetMapping("/deleteProject")
    public Boolean deleteProject(String project_id){
        return projectService.deleteProject(project_id);
    }

    //将一个大创设置为不可见
    @GetMapping("/setProjectNotShow")
    public Boolean setProjectNotShow(String project_id){
        return projectService.setProjectNotShow(project_id);
    }

    //获取所有的用户信息
    @GetMapping("/getAllUsers")
    public String getAllUsers() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(userService.findAll());
    }

    //根据类型获取信息列表
    @GetMapping("/getListByType")
    public String getListByType(String type) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(userService.findByType(type));
    }

    //根据用户的ID修改用户的信息
    @GetMapping("/updateUserInfo")
    public Boolean updateUserInfo(String item,String value,String userID){
        return userService.setUserName(userID,value,item);
    }

    //添加学生
    @PostMapping("/addStudent")
    public Boolean addStudent(@RequestBody Student student){
        return studentService.addStudent(student);
    }

    //根据Id删除指定的学生
    @GetMapping("/deleteStudent")
    public Boolean deleteStudent(String student_id){
        return studentService.deleteStudent(student_id);
    }

    //添加老师
    @PostMapping("/addTeacher")
    public Boolean addTeacher(@RequestBody Teacher teacher){
        return teacherService.addTeacher(teacher);
    }

    //根据指定的ID删除老师
    @GetMapping("/deleteTeacher")
    public Boolean deleteTeacher(String teacher_id){
        return teacherService.deleteTeacher(teacher_id);
    }

    //根据notice_id返回Notice_user记录
    @GetMapping("/getAllNoticeUser")
    public String getAllNoticeUser(String notice_id) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(noticeUserService.selectById(notice_id));
    }

    //修改用户界面公告已读
    @GetMapping("/haveRead")
    public Boolean haveRead(String user_id){
        return noticeUserService.updateNotice(user_id);
    }

    //查看用户是否已经读了最后一条公告
    @GetMapping("/getIsRead")
    public Boolean getIsRead(String user_id,String notice_id){
        return noticeUserService.isRead(user_id,notice_id);
    }
}
